package net.sky.oa.dao.docs.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sky.oa.dao.SkyBaseDaoImpl;
import net.sky.oa.dao.docs.FileAttachDao;
import net.sky.oa.model.FileAttach;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.htsoft.core.web.paging.PagingBean;

@SuppressWarnings("unchecked")
@Repository
public class FileAttachDaoImpl extends SkyBaseDaoImpl<FileAttach> implements
		FileAttachDao {
	public FileAttachDaoImpl() {
		super.setPersistType(FileAttach.class);
	}

	public void removeByPath(final String filePath) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session
						.createQuery("delete from FileAttach fa where fa.filePath = ?");
				query.setString(0, filePath);
				return Integer.valueOf(query.executeUpdate());
			}
		});
	}

	public FileAttach getByPath(String filePath) {
		String hql = "from FileAttach fa where fa.filePath = ?";
		return ((FileAttach) findUnique(hql, new Object[] { filePath }));
	}

	public List<FileAttach> findByParams(int qType, Long folderId, Long userId,
			Long id, String fileName, String createtime, PagingBean pb) {
		StringBuilder hql = new StringBuilder("");// 个人文档
		List list = new ArrayList();
		if (qType == 1 || qType == 2|| qType == 3)// qType等于1时,查询个人文档,qType等于2时,查询公共文档及部门文档
		{
			hql.append("select new FileAttach(fa.id,fa.fileName,fa.filePath,fa.createtime,fa.ext,fa.fileType,fa.note,fa.creator,fa.status,fa.ispublic) from FileAttach fa where fa.status=1 ");
			if (0 < folderId) {
				hql.append(" and fa.docFolder.id=?");
				list.add(folderId);
			}else
			{
				if(qType==2)//部门文档
				{
					hql.append(" and fa.docFolder.type=2");
				}
				if(qType==3)//公共文档
				{
					hql.append(" and fa.docFolder.type=1");
				}
			}
		}
		if (qType == 1)// qType等于1时,查询个人文档
		{
			if (-1 != userId) {
				hql.append(" and fa.user.id=?");
				list.add(userId);
			}
		}
		if (qType == 4)// 我的收藏
		{
			hql.append("select new FileAttach(fa.id,fa.fileName,fa.filePath,fa.createtime,fa.ext,fa.fileType,fa.note,fa.creator,fa.status,fa.ispublic) from FileAttach fa join fa.fusers as fu where fu.id=?");
			list.add(userId);
		}
		/*if (qType == 5)// 查收文档
		{
			hql.append("select fa from FileAttach fa join fa.docFileUsers as fd where fd.rusr.id=?");
			list.add(userId);
		}*/
		if (qType == 6)// 共享文档
		{
			hql.append("select new FileAttach(fa.id,fa.fileName,fa.filePath,fa.createtime,fa.ext,fa.fileType,fa.note,fa.creator,fa.status,fa.ispublic) from FileAttach fa where fa.ispublic=2");
		}
		if (-1 != id) {
			hql.append(" and fa.id=?");
			list.add(id);
		}
		if (StringUtils.isNotBlank(fileName)) {
			hql.append(" and fa.fileName like ?");
			list.add("%" + fileName + "%");
		}
		if (StringUtils.isNotBlank(createtime)) {
			hql.append(" and to_char(fa.createtime,'yyyy-MM-dd')=?");
			list.add(createtime);
		}
		/*if(qType==4){
			hql.append(" order by fd.sendtime desc ");
		}else{*/
			hql.append(" order by fa.createtime desc ");
//		}
		return findByHql(hql.toString(), list.toArray(), pb);
	}

	public int findRelation(final Long id) {
		int count=0;
		BigDecimal favcount= (BigDecimal) getHibernateTemplate().execute(new HibernateCallback() {
			public BigDecimal doInHibernate(final Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"select count(*) from file_user where file_id=" + id);
				Query query = session.createSQLQuery(sb.toString());
				return (BigDecimal)query.uniqueResult();
			}
		});
		BigDecimal sndcount=(BigDecimal) getHibernateTemplate().execute(new HibernateCallback() {
			public BigDecimal doInHibernate(final Session session)
			throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(
						"select count(*) from doc_file_user where file_id=" + id);
				Query query = session.createSQLQuery(sb.toString());
				return (BigDecimal)query.uniqueResult();
			}
		});
		count+=favcount.intValue();
		count+=sndcount.intValue();
		return count;
		
	}

}